package handle

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
)

func NewFile() File {
	return File{}
}

type File struct {
}

func (r File) InitRoute(server *fiber.App) {
	p := server.Group("/file")
	p.Post("/upload", r.Upload())
	p.Post("/uploads", r.FormFiles())
	p.Post("/uploadFormFiles", r.UploadFormFiles())
	p.Post("/sendFile", r.SendFile())
}

func (r File) Upload() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		// Set a lower memory limit for multipart forms (default is 32 MiB)
		// OR
		// app.Use(iris.LimitRequestBodySize(maxSize))
		// OR
		// OR iris.WithPostMaxMemory(maxSize)

		// single file
		fileHeader, err := ctx.FormFile("file")
		if err != nil {
			return ctx.JSON(err)
		}
		fmt.Println(fileHeader.Header, fileHeader.Size)
		return nil
	}
}
func (r File) FormFiles() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		files, err := ctx.MultipartForm()
		if err != nil {
			ctx.Status(400)
			return ctx.JSON(err)
		}
		for s, headers := range files.File {
			fmt.Println(s, headers)
			for i, header := range headers {
				fmt.Println(i, header.Header)
			}
		}
		for s, headers := range files.Value {
			fmt.Println(s, headers)
		}
		return nil
	}
}
func (r File) UploadFormFiles() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		//ctx.SaveUploadedFile()

		return nil
	}
}
func (r File) SendFile() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return nil
	}
}
